﻿using System;
using System.Collections.Generic;
using AppSocketModel;
using AppSocketService.Fence;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BCGPSRESTful.Controllers.Phone.Fence
{
    /// <summary>
    /// 电子围栏
    /// </summary>
    [ApiExplorerSettings(GroupName = "PersonApp")]
    [Route("api/[controller]")]
    [ApiController]
    public class PhoneFenceController :  PhoneBaseController
    {
        private readonly IFencePersonService fencePersonService;
        #region    构造函数
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="fencePersonService"></param>
        public PhoneFenceController(IFencePersonService fencePersonService)
        {
            this.fencePersonService = fencePersonService;
        }
        #endregion
        #region 添加围栏
        /// <summary>
        /// 添加围栏
        /// </summary>
        /// <param name="fence_request_dto"></param> 
        [HttpPost("[action]")]
        [ProducesResponseType(typeof(FenceDto), 200)]
        public ActionResult<FenceDto> Add(FenceRequestDto fence_request_dto)
        {
            return fencePersonService.AddNew(fence_request_dto);
        }
        #endregion

        #region 修改围栏
        /// <summary>
        /// 修改围栏
        /// </summary>
        /// <param name="fence_dto"></param>
        /// <param name="gps_map_type">地图种类</param>
        /// <returns></returns>
        [HttpPut("[action]")]
        [ProducesResponseType(typeof(FenceDto), 200)]
        public ActionResult<FenceDto> Update(FenceDto fence_dto, GpsMapType? gps_map_type = GpsMapType.GDMAP)
        {
            return fencePersonService.Update(gps_map_type.Value, fence_dto);
        }
        #endregion

        #region 删除全部围栏 --- 栏注销账户用
        /// <summary>
        /// 删除全部围栏 --- 栏注销账户用
        /// </summary> 
        /// <returns></returns>
        [HttpDelete("[action]")]
        [ProducesResponseType(typeof(void), 200)]
        public ActionResult DeleteAll_UnRegisterAccount()
        {
            fencePersonService.DeleteAll();
            return Ok();
        }
        #endregion

        #region 删除指定设备的全部围栏
        /// <summary>
        /// 删除指定设备的全部围栏
        /// </summary>
        /// <param name="imei"></param>
        /// <returns></returns>
        [HttpDelete("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public ActionResult<bool> DeleteAll(string imei)
        {
            var a = fencePersonService.DeleteAll(imei);
            if (a > 0)
                return true;
            else
                throw new ArgumentException("没有任何数据被删除!");

        }
        #endregion

        #region 删除围栏
        /// <summary>
        /// 删除围栏
        /// </summary>
        /// <param name="fence_id">围栏Id</param>
        /// <returns></returns>
        [HttpDelete("[action]")]
        [ProducesResponseType(typeof(bool), 200)]
        public ActionResult<bool> Delete(string fence_id)
        {
            var a = fencePersonService.Delete(fence_id);
            if (a > 0)
                return true;
            else
                throw new ArgumentException("没有任何数据被删除!");

        }
        #endregion

        #region 查询全部
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <param name="imei"></param>
        /// <param name="gps_map_type">地图种类</param>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(List<FenceDto>), 200)]
        public ActionResult<List<FenceDto>> QueryAll(string imei, GpsMapType? gps_map_type = GpsMapType.GDMAP)
        {
            return fencePersonService.GetAll(gps_map_type.Value, imei);
        }
        #endregion

    }
}